﻿Imports System.Object
Imports VSModel
Imports Cls_Common

Partial Class Control_Contents_Detail_Video
    Inherits System.Web.UI.UserControl
    Private strFileUrl As String
    Private strUrlYoutube As String
    Private strPlayerUrl As String
    'System.Configuration.ConfigurationManager.AppSettings("player_url")
    Private strVideoTitle As String

    Public Property UrlYoutube() As String
        Get
            Return strUrlYoutube
        End Get
        Set(ByVal Value As String)
            strUrlYoutube = Value
        End Set
    End Property
    Public Property FileUrl() As String
        Get
            Return strFileUrl
        End Get
        Set(ByVal Value As String)
            strFileUrl = Value
        End Set
    End Property
    Public Property PlayerUrl() As String
        Get
            Return strPlayerUrl
        End Get
        Set(ByVal Value As String)
            strPlayerUrl = Value
        End Set
    End Property
    Public Property VideoTitle() As String
        Get
            Return strVideoTitle
        End Get
        Set(ByVal Value As String)
            strVideoTitle = Value
        End Set
    End Property


    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not IsPostBack Then
            If Not Request.QueryString("Id") Is Nothing Then
                  hidID.Value = Request.QueryString("Id")
                ShowData()
            End If
        End If
    End Sub
    Protected Sub ShowData()
        Using data As New VSEntities
            Dim p = (From q In data.Contents Where q.Id = hidID.Value Select q).SingleOrDefault
            If Not p Is Nothing Then
                hplTitle.Text = IIf(IsNothing(p.Title) = True, "", p.Title)
                ltrDes.Text = IIf(IsNothing(p.Description) = True, "", p.Description)
                imgNews.ImageUrl = p.Image.ToString.Replace("~/Uploads", "")
                lblAuthor.Text = p.Author.ToString
                lblCreated.Text = String.Format("{0:d/M/yyyy}", p.Created)
                hplType.Text = arrTypeText(p.TypeId)
                hplType.CssClass = arrTypeCssClass(p.TypeId)
                If p.IsYoutube = True Then
                    pnlVideoUpload.Visible = False
                    pnlYoutube.Visible = True
                    'VideoTitle() = p.Title
                    'FileUrl() = SplitVideoYoutube(p.URL)
                    ltrShow.Text = p.URL
                Else

                    Dim baseUrl As String = Request.Url.GetLeftPart(UriPartial.Authority)
                    VideoTitle() = p.Title
                    PlayerUrl() = baseUrl + ResolveUrl_Video("~/jwplayer/player.swf")
                    FileUrl() = p.URL
                    pnlVideoUpload.Visible = True
                    pnlYoutube.Visible = False
                End If
                ltrView.Text = "Lượt xem " & IIf(IsNothing(p.Viewed), 0, p.Viewed).ToString
            End If
            Dim h = (From k In data.Contents Where k.MenuId = p.MenuId And k.Id <> p.Id And k.TypeId = 2 Select k.Id, k.Title, k.Created, k.TypeId Order By Id Descending).Take(5).ToList
            If Not h Is Nothing Then
                grvData.DataSource = h
                grvData.DataBind()
            End If
        End Using
    End Sub
    'Protected Sub Player(ByVal strUrl As String, ByVal strName As String)
    '    ltrPlayer.Text = " <script type=""text/javascript"" language=""javascript"">"
    '    ltrPlayer.Text += "     $(function () {"
    '    ltrPlayer.Text += "     $("".alejandro"").click(function () {"
    '    ltrPlayer.Text += "          var fileurl = $(this).attr(""name"");"
    '    ltrPlayer.Text += "        var title = $(this).attr(""innerHTML"");"
    '    ltrPlayer.Text += "       var newDivTitle = '<span class=""lblEdit"">' + title + '</span>';"
    '    ltrPlayer.Text += "          var newObjElement = '<embed width=""300"" height=""300"" flashvars=""file=' + fileurl + '"" allowfullscreen=""true"" allowscriptaccess=""always"" src=""/CODING/jwplayer/player.swf"" name=""player2"" id=""player2"" type=""application/x-shockwave-flash"">';"
    '    ltrPlayer.Text += "       $(""#divEmbededVideo"").html(newDivTitle + newObjElement);"
    '    ltrPlayer.Text += "   });"
    '    ltrPlayer.Text += "     }); "
    '    ltrPlayer.Text += "     </script>"
    '    ltrPlayer.Text += " <div id=""divEmbededVideo"" style=""padding-left:10px; padding-top:10px;"">"

    '    ltrPlayer.Text += "       <span class=""lblEdit"">" + strName + "</span>"
    '    ltrPlayer.Text += "        <!-- START OF THE PLAYER EMBEDDING TO COPY-PASTE -->"
    '    ltrPlayer.Text += "      <embed "
    '    ltrPlayer.Text += "     Type = ""application/x-shockwave-flash"""
    '    ltrPlayer.Text += "               src='" + IIf(strUrl.ToString.Contains("http://"),, ResolveUrl_Video("~/jwplayer/player.swf") + "'"
    '    ltrPlayer.Text += "       width = ""300"""
    '    ltrPlayer.Text += "     height = ""300"""
    '    ltrPlayer.Text += "   Style = ""undefined"""
    '    ltrPlayer.Text += "    ID = ""player"""
    '    ltrPlayer.Text += "    name = ""player"""
    '    ltrPlayer.Text += "    bgcolor = ""#000000"""
    '    ltrPlayer.Text += "   quality = ""high"""
    '    ltrPlayer.Text += "   allowfullscreen = ""true"""
    '    ltrPlayer.Text += "   allowscriptaccess = ""always"""
    '    ltrPlayer.Text += " wmode = ""opaque"""
    '    ltrPlayer.Text += "       flashvars=""file=../Uploads/Video/Video Gallery.flv""/>"
    '    ltrPlayer.Text += "    <script type=""text/javascript"">"
    '    ltrPlayer.Text += "     var so = new SWFObject('" + ResolveUrl_Video("~/jwplayer/player.swf") + "')', 'player', '300', '300', '9', '#000000');"
    '    ltrPlayer.Text += "     so.addParam('allowfullscreen', 'true');"
    '    ltrPlayer.Text += "      so.addParam('allowscriptaccess', 'always');"
    '    ltrPlayer.Text += "      so.addParam('wmode', 'opaque');"
    '    ltrPlayer.Text += "      so.addParam('flashvars', 'file=../Uploads/Video/Video Gallery.flv');"
    '    ltrPlayer.Text += "   so.write('preview');"
    '    ltrPlayer.Text += "      swfobject.registerObject(""FlashID"");"
    '    ltrPlayer.Text += "    </script>	        "
    '    ltrPlayer.Text += "     <!-- END OF THE PLAYER EMBEDDING -->        "
    '    ltrPlayer.Text += "  </div>    "
    'End Sub
    Public Function ResolveUrl_Video(ByVal url As String) As String
        If url Is Nothing Then
            Throw New ArgumentNullException("url", "url can not be null")
        End If
        If url.Length = 0 Then
            Throw New ArgumentException("The url can not be an empty string", "url")
        End If

        ' there is no ~ in the first character position, just return the url 
        If url(0) <> "~"c Then
            Return url
        End If

        Dim applicationPath As String = HttpContext.Current.Request.ApplicationPath

        ' there is just the ~ in the URL, return the applicatonPath 
        If url.Length = 1 Then
            Return applicationPath
        End If

        ' assume url looks like ~somePage 
        Dim indexOfUrl As Integer = 1

        ' determine the middle character 
        Dim midPath As String = If((applicationPath.Length > 1), "/", String.Empty)

        ' if url looks like ~/ or ~\ change the indexOfUrl to 2 
        If url(1) = "/"c OrElse url(1) = "\"c Then
            indexOfUrl = 2
        End If

        Return applicationPath & midPath & url.Substring(indexOfUrl)
    End Function
    Protected Function getVideoYoutube(ByVal ID As String) As String
        Dim scr As String = "<object width='320' height='240'> "

        scr = scr & "<param name='movie' value='" + "http://www.youtube.com/v/" & ID & "'></param> "

        scr = scr & "<param name='allowFullScreen' value='true'></param> "

        scr = scr & "<param name='allowscriptaccess' value='always'></param> "

        scr = scr & "<embed src='" + "http://www.youtube.com/v/" & ID & "'"

        scr = scr & "type='application/x-shockwave-flash' allowscriptaccess='always' "

        scr = scr & "allowfullscreen='true' width='320' height='240'> "

        scr = scr & "</embed></object>"
        Return scr
    End Function
    Protected Function SplitVideoYoutube(ByVal strUrl As String) As String
        Dim intStart As Integer = strUrl.IndexOf("=")
        Dim intEnd As Integer = strUrl.IndexOf("&")
        Dim scr As String = strUrl.Substring(intStart + 1, strUrl.Length - intStart - 1)
        Return scr
    End Function
End Class
